from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from asr import router as asr_router
from chat import router as tts_router
from keywords import router as keywords_router
from logger import logging

# 初始化FastAPI应用
app = FastAPI()

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(asr_router, tags=["asr"])
app.include_router(tts_router, tags=["tts"])
app.include_router(keywords_router, tags=["keywords"])

if __name__ == "__main__":
    import uvicorn
    from dotenv import load_dotenv
    load_dotenv()
    import os
    if os.getenv("API_KEY") == '' or os.getenv("API_KEY") is None:
        logging.error("API_KEY未设置，请在环境变量中设置API_KEY")
        exit(1)
    port = int(os.getenv("PORT", 7000))
    logging.info(f"启动服务器，端口: {port}")
    uvicorn.run(app, host="0.0.0.0", port=port)
